package com.asia.algorithmcode.linkedList;

import java.util.HashSet;
import java.util.Set;

/**
 * 142，环形链表2
 */
public class DetectCycle {


    public ListNode detectCycle(ListNode head) {
        if (head == null || head.next == null) {
            return null;
        }

        Set<ListNode> set = new HashSet<>();
        while (head != null) {
            if (set.contains(head)) {
                return head;
            } else {
                set.add(head);
            }
            head = head.next;
        }
        return null;
    }

}
